﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using MLM.Data;
using MLM.AdminCP;
namespace MLM.Home.Pages
{
    public partial class ViewCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
                BindData();
            btnPayMentCart.Click += new EventHandler(btnPayMentCart_Click);

        }

        void btnPayMentCart_Click(object sender, EventArgs e)
        {

            Invoice _Invoice = new Invoice();

            _Invoice.DepoID = MLM.ShoppingCart.Instance.Items.FirstOrDefault().USerID; ;
            _Invoice.DateTimeCreated = DateTime.Now;
            _Invoice.UserID = Permission.GetCurrentUserId();
            _Invoice.Status = -1;
            using (var DataContext = new MLMDataContext())
            {
                DataContext.Invoices.AddObject(_Invoice);
                DataContext.SaveChanges();
                var invoiceID = _Invoice.InvoiceID;
                foreach (var item in MLM.ShoppingCart.Instance.Items)
                {
                    if (invoiceID > 0)
                    {
                        InvoiceDetail _InvoiceDetail = new InvoiceDetail();
                        try
                        {
                            //var query = DataContext.InvoiceDetails.Where(p => p.ProductBaseID == item.ProductId).ToList();
                            //if (query.Count > 0)
                            //{
                            //    _InvoiceDetail.Quantity = query.FirstOrDefault().Quantity + item.Quantity;
                            //}
                            //else
                            //{
                            _InvoiceDetail.InvoiceID = long.Parse(invoiceID.ToString());
                            _InvoiceDetail.Point = long.Parse(item.Points);
                            _InvoiceDetail.ProductBaseID = item.ProductId;
                            _InvoiceDetail.Price = double.Parse(item.UnitPrice.ToString());
                            _InvoiceDetail.VAT = double.Parse(item.VAT.ToString());
                            _InvoiceDetail.Quantity = item.Quantity;
                            DataContext.InvoiceDetails.AddObject(_InvoiceDetail);
                            // }
                            DataContext.SaveChanges();

                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
                    }
                }
            }
            MLM.ShoppingCart.Instance.Items.Clear();
            BindData();
            Message.ShowSuccess("Cart Out success !");
        }

        public void BindData()
        {
            gvShoppingCart.DataSource = MLM.ShoppingCart.Instance.Items;
            gvShoppingCart.DataBind();
        }

        protected void gvShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        /**
         * Thực hiện xóa item trong giỏ hàng
         */
        protected void gvShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                int productId = Convert.ToInt32(e.CommandArgument);
                MLM.ShoppingCart.Instance.RemoveItem(productId);
            }

            // Hiển thị lại dữ liệu
            BindData();
        }
        //Thực hiện update giỏ hàng 
        protected void btnUpdateCart_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvShoppingCart.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        // Lấy productId
                        int productId = Convert.ToInt32(gvShoppingCart.DataKeys[row.RowIndex].Value);
                        // Tìm và lấy giá trị của số lượng
                        int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
                        MLM.ShoppingCart.Instance.SetItemQuantity(productId, quantity);
                    }
                    catch (FormatException) { }
                }
            }

            BindData();
        }
    }
}